package com.pf.business.controller;

import com.pf.business.mapper.entity.User;
import com.pf.business.service.RedisService;
import com.pf.business.service.UserService;
import com.pf.utils.FromRedisOrDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author PF
 * @date 2021/5/15 14:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private UserService userService;

    /**
     * 一个手机号一天只能发送10条短信
     *
     * @param mobile
     * @return
     */
    @GetMapping("/checkNumsInOneDay")
    public String sendSmsVerificationCode(@RequestParam("mobile") String mobile) {
        redisService.sendSmsVerificationCode(mobile);
        return "ok";
    }

    /**
     * @param userId
     * @return
     */
    @GetMapping("/test")
    public User test(@RequestParam("userId") Integer userId) {
        String key = "user_" + userId;
        User user = FromRedisOrDB.getObject(key, User.class, () -> {
            return userService.getUser(userId);
        }, 120);
        return user;
    }

}
